package com.appbee.core.client.rpcfilter.chain;

import com.appbee.core.client.rpcfilter.RpcFilter;
import com.appbee.core.client.rpcfilter.RpcFilterChain;
import com.appbee.core.client.rpcfilter.filters.CacheFilter;
import com.appbee.core.client.rpcfilter.filters.CancelSameValueFilter;
import com.appbee.core.client.rpcfilter.filters.MinLengthFilter;
import com.appbee.core.client.rpcfilter.filters.MonoSyncFilter;

public class ChainBuilder<I, R>
{
	protected RpcFilterChainImpl<I, R> firstChain;

	protected RpcFilterChainImpl<I, R> currentChain;

	public ChainBuilder<I, R> chainCancelSameValue(I currentValue)
	{
		CancelSameValueFilter<I, R> filter = new CancelSameValueFilter<I, R>(
				currentValue);
		return chain(filter);
	}

	public RpcFilterChain<I, R> getFirstChain()
	{
		return firstChain;
	}

	public ChainBuilder<I, R> chainMinLength(int minLength)
	{
		MinLengthFilter<I, R> filter = new MinLengthFilter<I, R>(minLength);
		return chain(filter);
	}

	public ChainBuilder<I, R> chainMonoSync()
	{
		MonoSyncFilter<I, R> filter = new MonoSyncFilter<I, R>();
		return chain(filter);
	}

	public ChainBuilder<I, R> chainCache(int cacheTime, int capcity)
	{
		CacheFilter<I, R> filter = new CacheFilter<I, R>(cacheTime, capcity);
		return chain(filter);
	}

	public RpcFilterChain<I, R> chainLast(RpcFilter<I, R> filter)
	{
		currentChain = currentChain.chain(filter);
		return firstChain;
	}

	public RpcFilterChain<I, R> chainLast(RpcFilterChain<I, R> chain)
	{
		currentChain.setChain(chain);
		return firstChain;
	}

	public ChainBuilder<I, R> chain(RpcFilter<I, R> filter)
	{
		if (firstChain == null)
		{
			firstChain = new RpcFilterChainImpl<I, R>(filter);
			currentChain = firstChain;
		}
		else
		{
			currentChain = currentChain.chain(filter);
		}
		return this;
	}
}
